package com.xuecheng.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.dto.BindTeachplanMediaDTO;
import com.xuecheng.dto.SaveTeachplanDTO;
import com.xuecheng.exception.XueChengPlusException;
import com.xuecheng.mapper.TeachplanMapper;
import com.xuecheng.mapper.TeachplanMediaMapper;
import com.xuecheng.po.Teachplan;
import com.xuecheng.po.TeachplanMedia;
import com.xuecheng.service.TeachplanService;
import com.xuecheng.vo.TeachplanVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class TeachplanServiceImpl extends ServiceImpl<TeachplanMapper, Teachplan> implements TeachplanService {
	
	@Resource
	private TeachplanMapper teachplanMapper;
	@Resource
	private TeachplanMediaMapper teachplanMediaMapper;
	
	@Override
	public List<TeachplanVO> findTeachplanTree(Long courseId) {
		return teachplanMapper.selectTreeNodes(courseId);
	}
	
	//新增、修改
	@Override
	public void saveTeachplan(SaveTeachplanDTO saveTeachplanDTO) {
		Teachplan teachplan = teachplanMapper.selectById(saveTeachplanDTO.getId());
		if (teachplan == null) {
			teachplan = new Teachplan();
			BeanUtils.copyProperties(saveTeachplanDTO, teachplan);
			//找到同级课程计划的数量
			int count = getTeachplanCount(saveTeachplanDTO.getCourseId(), saveTeachplanDTO.getParentid());
			//新课程计划的值
			teachplan.setOrderby(count + 1);
			
			teachplanMapper.insert(teachplan);
			
		} else {
			BeanUtils.copyProperties(saveTeachplanDTO, teachplan);
			//更新
			teachplanMapper.updateById(teachplan);
			
		}
		
		
	}
	
	@Transactional
	@Override
	public TeachplanMedia associationMedia(BindTeachplanMediaDTO bindTeachplanMediaDTO) {
		//教学计划的id
		Long teachplanId = bindTeachplanMediaDTO.getTeachplanId();
		Teachplan teachplan = teachplanMapper.selectById(teachplanId);
		//约束校验
		//教学计划不存在无法绑定
		if (teachplan == null) {
			XueChengPlusException.cast("教学计划不存在");
		}
		//只有二级目录才可以绑定视频
		Integer grade = teachplan.getGrade();
		if (grade != 2) {
			XueChengPlusException.cast("只有二级目录才可以绑定视频");
		}
		
		
		//删除原来的绑定关系
		LambdaQueryWrapper<TeachplanMedia> lambdaQueryWrapper =
			new LambdaQueryWrapper<TeachplanMedia>().eq(TeachplanMedia::getTeachplanId, teachplanId);
		teachplanMediaMapper.delete(lambdaQueryWrapper);
		
		//添加新的绑定关系
		TeachplanMedia teachplanMedia =
			TeachplanMedia.builder().teachplanId(teachplanId).mediaFilename(bindTeachplanMediaDTO.getFileName()).mediaId(bindTeachplanMediaDTO.getMediaId()).createDate(LocalDateTime.now()).courseId(teachplan.getCourseId()).build();
		teachplanMediaMapper.insert(teachplanMedia);
		return teachplanMedia;
	}
	
	//计算机新课程计划的orderby 找到同级课程计划的数量 SELECT count(1) from teachplan where course_id=117 and parentid=268
	public int getTeachplanCount(Long courseId, Long parentId) {
		LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(Teachplan::getCourseId, courseId);
		queryWrapper.eq(Teachplan::getParentid, parentId);
		return teachplanMapper.selectCount(queryWrapper);
		
	}
}
